/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.manage.web.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import net.mikoo.seals.biz.service.ApplyServices;
import net.mikoo.seals.biz.service.DevicesTakeService;
import net.mikoo.seals.biz.service.DevicesUsedServices;
import net.mikoo.seals.biz.service.OrganizationService;
import net.mikoo.seals.biz.service.PartyServices;
import net.mikoo.seals.biz.service.PersonServices;
import net.mikoo.seals.biz.service.StaffServices;
import net.mikoo.seals.biz.service.UserLoginServices;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.enums.GenderEnum;
import net.mikoo.seals.common.enums.PartyStatusEnum;
import net.mikoo.seals.common.enums.PartyTypeEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.manage.web.common.helper.FormHelper;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyQuery;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;
import net.mikoo.seals.model.Collections;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.DevicesTakeQuery;
import net.mikoo.seals.model.DevicesUsed;
import net.mikoo.seals.model.DevicesUsedQuery;
import net.mikoo.seals.model.Organization;
import net.mikoo.seals.model.OrganizationParty;
import net.mikoo.seals.model.OrganizationQuery;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.PartyQuery;
import net.mikoo.seals.model.Person;
import net.mikoo.seals.model.Staff;
import net.mikoo.seals.model.StaffQuery;
import net.mikoo.seals.model.UserLogin;
import net.mikoo.seals.utils.FileUtil;
import net.mikoo.seals.utils.MD5Util;

/**
 * 
 * @author huangxiaoming
 *
 */
@Controller
@RequestMapping("/staff")
public class StaffController extends BaseController {

	Logger logger = Logger.getLogger(StaffController.class);
	
	@Autowired
	private ServletContext servletContext;

	@Autowired
	private UserLoginServices userLoginServices;

	@Autowired
	private PartyServices partyServices;
	
	@Autowired
	private PersonServices personServices;
	
	@Autowired
	private OrganizationService organizationService;
	
	@Autowired
	private ApplyServices applyServices;
	
	@Autowired
	private VerifyService verifyService;
	
	@Autowired
	private DevicesUsedServices devicesUsedServices;
	
	@Autowired
	private DevicesTakeService devicesTakeService;
	
	@Autowired
	private StaffServices staffServices;
	
	
	
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request, Model model) {
		
		return "staff/index";
	}
	
	/**
	 * 员工列表
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String staffs(PartyQuery query,HttpServletRequest request, Model model) {
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Party> pm = partyServices.getPageByQuery(query);
		
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		if(query.getOrganizationId() != null) {
			Organization org = organizationService.getInfoById(query.getOrganizationId());
			model.addAttribute("org", org);
		}
		
		return "staff/list";
	}
	
	/**
	 * 机构列表
	 * @param query
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/orgs")
	public String orgs(OrganizationQuery query,Model model, HttpServletRequest request){
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getListByQuery(query);
		model.addAttribute("orgs", orgs);
		return "staff/orgs";
	}
	
	/**
	 * 设置员工
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/setting")
	public String setting(OrganizationQuery query ,HttpServletRequest request, Model model) {
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getTreeByQuery(query);
		for (Organization organization : orgs) {
			System.out.println(organization.getName() + organization.getChildren().size());
		}
		model.addAttribute("orgs", orgs);
		
		return "staff/setting";
	}
	
	
	
	@RequestMapping("/setting/post")
	@ResponseBody Map<String, Object> settingPost(Collections parms, HttpServletRequest request){
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			organizationService.addStaff(parms.getOrgId(),parms.getIds(),companyId);
			return result(SUCCESS, "移动成功");
		}catch(ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
 

	
	/**
	 * 移除机构员工
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("/remove")
	@ResponseBody Map<String, Object> remove(OrganizationParty orgParty, HttpServletRequest request){
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			orgParty.setCompanyId(companyId);
			organizationService.removeStaff(orgParty);
			return result(SUCCESS, "删除成功");
		}catch(ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	

	/**
	 * 添加操作员
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/form")
	public String form(HttpServletRequest request, Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		OrganizationQuery query = new OrganizationQuery();
		query.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getTreeByQuery(query);
		model.addAttribute("orgs", orgs);

		
		return "staff/form";
	}

	/**
	 * 创建操作员
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/create")
	@ResponseBody
	public Map<String, Object> create(HttpServletRequest request, Model model) {

		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);


			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			FormHelper form = new FormHelper();
			Party party = form.getCreatOperator(request);
			party.setCompanyId(companyId);
			party.setCreatedBy(partyId);
			party.setIsSystem("N");

			partyServices.create(party);
			return result(SUCCESS, MSG_SUCCESS);
		} catch (ParametersException e) {
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			return result(ERROR, MSG_SYSTEM_ERROR);
		}

	}

	
	@RequestMapping("/edit")
	public String edit(StaffQuery query,HttpServletRequest request, Model model) {
		Long companyId = getCompanyId(request, null);
		query.setCompanyId(companyId);
		
		Party party = partyServices.loadByPrimaryKey(query.getPartyId());
		model.addAttribute("party", party);
		
		Staff staff = staffServices.getInfoByQuery(query);
		model.addAttribute("staff", staff);
		
		
		OrganizationQuery orgQuery = new OrganizationQuery();
		orgQuery.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getTreeByQuery(orgQuery);
		model.addAttribute("orgs", orgs);
		if(orgs !=null && !orgs.isEmpty()) {
			orgQuery.setPartyId(query.getPartyId());
			Organization org = organizationService.getInfoWithPartyByQuery(orgQuery);
			
			model.addAttribute("org", org);
		}
		
		return "staff/edit";
	}
	

	/**
	 * 修改操作员
	 * 
	 * @param userLogin
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public Map<String, Object> modify(Staff staff,HttpServletRequest request, Model model) {

		try {
			
			Long companyId = getCompanyId(request, null);
			staff.setCompanyId(companyId);
			staffServices.modify(staff);
//			FormHelper form = new FormHelper();
//			Party party = form.getCreatOperator(request);
//			partyServices.modifyByPrimaryKey(party);
			return result(SUCCESS, "修改成功");

		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}
	}

	@RequestMapping("/changeStatus")
	@ResponseBody
	public Map<String, Object> changeStatus(Staff staff, Model model,HttpServletRequest request) {

		try {
			Long companyId = getCompanyId(request, null);
			staff.setCompanyId(companyId);
			staffServices.changeStatus(staff);
			
			return result(SUCCESS, MSG_SUCCESS);

		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}

	/**
	 * 删除操作员
	 * 
	 * @param partyId
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(Long partyId, Model model) {
		// // 删除当事人表
		// Result<Long> resultParty = partyServices.deleteByPrimaryKey(partyId);
		// // 删除Person表
		// Result<Long> resultPerson = personServices.deleteByPartyId(partyId);
		// // 删除用户登录表
		// Result<Long> resultUserLogin = userLoginServices.deleteByPartyId(partyId);
		// // 删除当事人角色表
		// Result<Long> resultPartyRole = partyRoleService.deleteByPartyId(partyId);
		// if(resultParty.isSuccess() && resultPerson.isSuccess() &&
		// resultUserLogin.isSuccess() && resultPartyRole.isSuccess()) {
		// return redirect("/admin/operator/index.htm");
		// }
		// return this.errorTips("", resultParty.getMessage() +
		// resultPerson.getMessage() + resultUserLogin.getMessage(), model);
		return null;
	}

	@RequestMapping("/verifyPassword")
	@ResponseBody
	public Integer verifyPassword(String currentPassword, HttpServletRequest request) {
		UserLogin userLogin = getLoginUser(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

		if (MD5Util.Md5(currentPassword).equals(userLogin.getCurrentPassword())) {
			return 1;
		}
		return 0;
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/editPassword")
	public String editPassword(HttpServletRequest request, Model model) {

		UserLogin userLogin = getLoginUser(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		model.addAttribute("userLogin", userLogin);
		return "staff/editPassword";
	}

	@RequestMapping("/modifyPassword")
	@ResponseBody
	public Map<String, Object> modifyPassword(HttpServletRequest request, Model model) {
		try {
			String userLoginId = getCurrentUserLoginId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			String currentPassword = request.getParameter("currentPassword");
			String newPassword = request.getParameter("newPassword");
			String confPassword = request.getParameter("confPassword");

			if (!newPassword.equals(confPassword)) {
				return result(ERROR, "两次密码输入不一致");
			}

			userLoginServices.changePassword(userLoginId, newPassword, currentPassword);

			removeSession(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			return result(SUCCESS, "修改成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}

	}
	
	
	@RequestMapping("/search")
	@ResponseBody
	public Map<String, Object> search(String searchKey, HttpServletRequest request){
		try {
			
			if(StringUtils.isBlank(searchKey)) {
				return result(SUCCESS, "查询成功", new ArrayList<>());
			}
			
			//Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			
			PartyQuery query = new PartyQuery();
			query.setSearchKey(searchKey);
			query.setSearchType("NAME_OR_USERLOGINID");
			//query.setCompanyId(companyId);
			
			List<Party> partys = partyServices.getListByQuery(query);
			
			Map<String, Object> data = new HashMap<>();
			
			if(partys != null && !partys.isEmpty()) {
				
				Party party = partys.get(0);
				data.put("partyId", party.getPartyId());
				data.put("userLoginId", party.getUserLoginId());
				data.put("name", party.getPerson().getName());
				data.put("portrait", party.getPerson().getPortrait());
				data.put("position", party.getPerson().getPosition());
				return result(SUCCESS, "查询成功", data);
			}
			return result("002", "用户不存在", data);
			
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR,e.getMessage());
		} catch (Exception e) {
			logger.error("系统出错！", e);
			return result(ERROR,MSG_SYSTEM_ERROR);
		}
	}
	
	/**
	 * 选择用户
	 * @param request
	 * @return
	 */
	@RequestMapping("/selected/index")
	public String selectedIndex(String type,Long id,Model model,HttpServletRequest request){
		
		model.addAttribute("type", type);
		model.addAttribute("id", id);
		return "staff/selected/index";
	}
	
	@RequestMapping("/selected/orgs")
	public String selectedOrgs(OrganizationQuery query,HttpServletRequest request,Model model){
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		query.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getListByQuery(query);
		model.addAttribute("orgs", orgs);
		
		return "staff/selected/orgs";
	}
	
	@RequestMapping("/selected/staffs")
	public String selectedStaffs(PartyQuery query,HttpServletRequest request,Model model){
			
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

		query.setCompanyId(companyId);
		query.setPartyType(PartyTypeEnum.OPERATOR.code());
		PageModel<Party> pm = partyServices.getPageByQuery(query);

		model.addAttribute("pm", pm);
		model.addAttribute("query", query);

		return "staff/selected/staffs";
	}
	
	
	@RequestMapping("/info")
	public String info(HttpServletRequest request,Model model){
			
		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Person person = personServices.getByPartyId(partyId);
		model.addAttribute("person", person);

		return "staff/info";
	}
	
//	@RequestMapping("/upportrait")
//	public String upportrait(HttpServletRequest request,Model model){
//		
//		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
//		Person person = personServices.getByPartyId(partyId);
//		model.addAttribute("person", person);
//		
//		return "staff/upportrait";
//	}
//	
//	@RequestMapping("/setting")
//	@ResponseBody
//	public Map<String, Object> setting(Person person,HttpServletRequest request,Model model){
//		
//		try {
//			Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
//			person.setPartyId(partyId);
//			personServices.modifyByPrimaryKey(person);
//			
//			Party loginParty = partyServices.loadByPrimaryKey(partyId);
//			
//			request.getSession().setAttribute(WebContext.ADMIN_LOGIN_SESSION_KEY, loginParty);
//			
//			return result(SUCCESS,"ok");
//		}catch (ParametersException e) {
//			logger.error("", e);
//			return result(ERROR,e.getMessage());
//		} catch (Exception e) {
//			logger.error("系统出错！", e);
//			return result(ERROR,MSG_SYSTEM_ERROR);
//		}
//	}
	
	@RequestMapping("/imports")
	@ResponseBody
	public Map<String, Object> imports(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request,Model model){
		
		try {
			Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
			int success = 0;
			int error = 0;
			List<Party> datas =  getInit(file.getInputStream());
			for (Party party : datas) {
				
				try {
					
					party.setCompanyId(companyId);
					party.setPartyId(partyId);
					party.setCreatedBy(partyId);
					partyServices.create(party);
					
					success += 1;
				}catch (Exception e) {
					logger.error("",e);
					error += 1;
				}
			}
			String msg = "成功导入"+success+"条,失败"+error+"条";
			
			return result(SUCCESS,msg);
		}catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR,e.getMessage());
		} catch (Exception e) {
			logger.error("系统出错！", e);
			return result(ERROR,MSG_SYSTEM_ERROR);
		}
	}
	
	
	private List<Party> getInit(InputStream in) throws Exception {
		
		List<Party> list = new ArrayList<>();
		Workbook wb0 = null;
		try {
			wb0= new HSSFWorkbook(in);
			Sheet sht0 = wb0.getSheetAt(0);
			
			
			for (Row r : sht0) {
				if (r.getRowNum() < 1) {
					continue;
				}
				
				Cell cell0 = r.getCell(0);
				cell0.setCellType(CellType.STRING);
				String name = cell0.getStringCellValue();
				
				Cell cell1 = r.getCell(1);
				cell1.setCellType(CellType.STRING);
				String gender = cell1.getStringCellValue();
				
				//Dept
				String dept = null;
				Cell cell2 = r.getCell(2);
				if(cell2 !=null) {
					cell2.setCellType(CellType.STRING);
					dept = cell2.getStringCellValue();
				}
			
				String position = null;
				Cell cell3 = r.getCell(3);
				if(cell3 !=null) {
					cell3.setCellType(CellType.STRING);
					position = cell3.getStringCellValue();
				}
				
				
				Cell cell4 = r.getCell(4);
				cell4.setCellType(CellType.STRING);
				String mobile = cell4.getStringCellValue();
				
				Cell cell5 = r.getCell(5);
				cell5.setCellType(CellType.STRING);
				String password = cell5.getStringCellValue();
				
				String email = null;
				Cell cell6 = r.getCell(6);
				if(cell6 !=null) {
					cell6.setCellType(CellType.STRING);
					email= cell6.getStringCellValue();
				}
				
				
				Party party = new Party();
				party.setUserLoginId(mobile);
				party.setPartyStatus(PartyStatusEnum.NORMAL.code());
				party.setCreatedTime(new Date());
				party.setIsSystem("N");

				UserLogin userLogin = new UserLogin();
				userLogin.setUserLoginId(mobile);
				userLogin.setCurrentPassword(password);
				userLogin.setIsSystem("N");
				userLogin.setEnabled("Y");
				userLogin.setRegisterIp("127.0.0.1");
				userLogin.setRegisterTime(new Date());
				party.setUserLogin(userLogin);
				
				
				GenderEnum ge = GenderEnum.getByMeaasge(gender);
				
				Person person = new Person();
				person.setUserLoginId(mobile);
				person.setName(name);
				person.setGender(ge!=null?ge.getCode():GenderEnum.UNKNOWN.code());
				person.setMobile(mobile);
				person.setEmail(email);
				person.setDept(dept);
				person.setPosition(position);
				party.setPerson(person);
				
				list.add(party);
				
			}
			
		} catch (Exception e) {
			logger.error("",e);
			throw e;
		}finally {
			try {
				wb0.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return list;
		
	}
	
	/**
	 * 下载上传模板
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/downtemp")
	public void downtemp(HttpServletRequest request,HttpServletResponse response) throws IOException{  
		
		InputStream  in = servletContext.getResourceAsStream("/temp/stafftemp.xls");
		
		byte[] data = FileUtil.toByteArray(in);
		
	    response.reset();  
	    response.setHeader("Content-Disposition", "attachment; filename=stafftemp.xls");  
	    response.addHeader("Content-Length", "" + data.length);  
	    response.setContentType("application/octet-stream;charset=UTF-8");  
	    OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
	    outputStream.write(data);  
	    outputStream.flush();  
	    outputStream.close();  
	}  
	
	
	@RequestMapping("/detail")
	public String detail(HttpServletRequest request,Model model){
			
		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Person person = personServices.getByPartyId(partyId);
		model.addAttribute("person", person);

		return "staff/detail";
	}
	
	
	/**
	 * 申请记录
	 * @param request
	 * @param response
	 */
	@RequestMapping("/apply")
	public String apply(ApplyQuery query,HttpServletRequest request,Model model){
		
		
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Apply> pm = applyServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		
		return "staff/apply";
	}
	
	/**
	 * 审核记录
	 * @param request
	 * @param response
	 */
	@RequestMapping("/verify")
	public String verify(ApplyVerifyQuery query,HttpServletRequest request,Model model){
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
		PageModel<ApplyVerify> pm = verifyService.getPageByQuery(query);
		model.addAttribute("pm", pm);
		return "staff/verify";
	}
	
	/**
	 * 使用记录
	 * @param request
	 * @param response
	 */
	@RequestMapping("/used")
	public String used(DevicesUsedQuery query,HttpServletRequest request,Model model){
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
		PageModel<DevicesUsed> pm = devicesUsedServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		return "staff/used";
	}
	
	
	/**
	 * 外带记录
	 * @param request
	 * @param response
	 */
	@RequestMapping("/take")
	public String take(DevicesTakeQuery query,HttpServletRequest request,Model model){
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
		PageModel<DevicesTake> pm = devicesTakeService.getPageByQuery(query);
		model.addAttribute("pm", pm);
		return "staff/take";
	}
	
}
